package org.urchr.beorganized.mobile.pages.login;

import org.apache.catalina.util.MD5Encoder;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.urchr.beorganized.mobile.base.Application;
import org.urchr.beorganized.mobile.base.MobileSession;
import org.urchr.beorganized.mobile.base.network.NetworkException;
import org.urchr.beorganized.mobile.base.network.XMLRequest;
import org.urchr.beorganized.mobile.base.page.BasePage;
import org.urchr.beorganized.mobile.pages.secured.browse.BrowsePage;
import org.w3c.dom.Element;

public class LoginPage extends BasePage{
	
	private String server = Application.DEFAULT_HOST, login = Application.DEFAULT_USER, password = "";
//	private String server = "beorganized.org", login = "", password = "";
	
	public LoginPage(PageParameters parameters) {
		super(parameters);
		
		Form form = new Form("form"){
			
			private static final long serialVersionUID = 868724882544232017L;

			@Override
			protected void onSubmit() {
				resetError();
				String apiURL = ((server.startsWith("http://") || server.startsWith("https://"))?"":"http://")+
						server+(server.endsWith("/")?"api/":"/api/");
				MobileSession.getSession().setServer(apiURL);
				XMLRequest request = new XMLRequest("user.CheckUserNamePassword");
				request.addParameter("userName", login);
				if(null!=password){
					request.addParameter("password", MD5.makeHash(password));
				}
				try {
					Element result = request.callOperation();
					String token = result.getAttribute("token");
//					System.out.println("Token: "+token);
					if(token!=null){
						MobileSession.getSession().setToken(token);
						MobileSession.getSession().setAuthorized(true);
						MobileSession.getSession().setDateFormat(result.getAttribute("dateFormat"));
						MobileSession.getSession().setTimeFormat(result.getAttribute("timeFormat"));
						MobileSession.getSession().setMinuteOffset(
								safeInt(result.getAttribute("minuteOffset"), 0));
						setResponsePage(BrowsePage.class);
					}
				} catch (NetworkException e) {
					showError(e.getErrorString());
				}
//				showError("Not finished yet!");
			}
		};
		form.setModel(new CompoundPropertyModel(this));
		form.add(new TextField<String>("server"));
		form.add(new TextField<String>("login"));
		form.add(new PasswordTextField("password").setRequired(false));
		form.add(new Button("submit"));
		add(form);
	}

	public void setServer(String server) {
		this.server = server;
	}

	public String getServer() {
		return server;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
}
